/**
 * created by 小卷毛, 2020-07-03 003
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.model.CommonModel
import com.zhiyou.aifeng.common_ui.utils.applyFlowableSchedulers
import com.zhiyou.aifeng.model.BrandData
import com.zhiyou.aifeng.model.WeatherModel
import io.reactivex.Flowable
import java.util.concurrent.TimeUnit

/**
 * 项目名称：Smart_Machine
 * 创建人：小卷毛
 * 创建时间：2020-07-03 15:46
 */
class MainViewModel : UpdateViewModel() {

    private val weatherKey = "f5d772166b9e4c4c8559062b6a6e9804"
    private val weatherApiKey = "6cc6d264d0ef463b9005cd1f90e0ccd3"

    fun getKey() = weatherKey

    fun startCount(): Flowable<Long> =
        Flowable.interval(1, TimeUnit.SECONDS)
            .compose(applyFlowableSchedulers())

    fun getHotline() =
        OkGo.post<BaseResponse<ArrayList<BrandData>>>(BaseUrl.brandType)
            .params(getParams("type" to "hotline"))
            .converter(object : JacksonConvert<BaseResponse<ArrayList<BrandData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    fun getLocationId(key: String) =
        OkGo.get<WeatherModel>("https://geoapi.heweather.net/v2/city/lookup")
            .params("location", key)
            .params("key", weatherApiKey)
            .params("range", "cn")
            .params("mode", "fuzzy")
            .converter(object : JsonConvert<WeatherModel>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.location }

    fun getMatterList(uid: String) =
        OkGo.post<BaseResponse<CommonModel>>(BaseUrl.workList)
            .params(
                getParams(
                    "uid" to uid,
                    "page" to 1
                )
            )
            .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.results }

}